package cn.bandeqing.worker.controller;

import cn.bandeqing.base.domain.CurrentWorkerUser;
import cn.bandeqing.base.resp.Resp;
import cn.bandeqing.frame.exception.CodeException;
import cn.bandeqing.stp.StpWorkerUtil;
import cn.bandeqing.worker.req.*;
import cn.bandeqing.worker.resp.WalletListResp;
import cn.bandeqing.worker.resp.WorkerRegisterResp;
import cn.bandeqing.worker.resp.WorkerWalletResp;
import cn.bandeqing.worker.rpc.IWorkRpc;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.hutool.core.lang.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Tag(name = "工作人员（收纳）")
@RequestMapping("/worker")
@RestController
public class WorkerController {


    @DubboReference
    private IWorkRpc workRpc;

    @Operation(summary = "提现")
    @PostMapping("/withdraw")
    public Resp<Void> withdraw(@RequestBody @Valid WithdrawReq req) {
        return workRpc.withdraw(req);
    }

    @Operation(summary = "上传设备信息")
    @PostMapping("/upload/equipment")
    public Resp<Void> uploadEquipment(@RequestBody @Valid UploadEquipmentReq req) {
        return workRpc.uploadEquipment(req);
    }

    @Operation(summary = "钱包流水")
    @PostMapping("/wallet/list")
    public Resp<List<WalletListResp>> walletList(@RequestBody @Valid WalletListReq req) {
        return workRpc.walletList(req);
    }

    @Operation(summary = "钱包")
    @PostMapping("/wallet")
    public Resp<WorkerWalletResp> wallet() {
        return workRpc.wallet();
    }

    @Operation(summary = "修改密码")
    @PostMapping("/modifyPwd")
    public Resp<Void> modifyPwd(@RequestBody @Valid ModifyPwdReq req) {
        Assert.isTrue(req.getNewPwd().equals(req.getNewPwdConfirm()), () -> new CodeException("新密码确认不一致"));
        Assert.isFalse(req.getOldPwd().equals(req.getNewPwd()), () -> new CodeException("新密码和老密码一样"));

        return workRpc.modifyPwd(req);
    }

    @Operation(summary = "当前用户")
    @PostMapping("/currnet")
    public Resp<CurrentWorkerUser> currnet() {
        CurrentWorkerUser current = StpWorkerUtil.getCurrent();
        return Resp.success(current);
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public Resp<Void> login(@RequestBody @Valid WorkerLoginReq req) {
        return workRpc.login(req);
    }

    @Operation(summary = "工作人员（收纳）注册")
    @PostMapping("/register")
    public Resp<WorkerRegisterResp> register(@RequestBody @Valid WorkerRegisterReq req) {
        return workRpc.register(req);
    }

}
